from django.shortcuts import render
from models_goods.oo import AllAssetName
from models_goods.models import *
from ribbon.views import permission
from django.http import HttpResponse


@permission
def in1(request):
    page, asset = request.GET.get('page'), request.GET.get('asset')  # 获取参数
    asset_name = AllAssetName().get_info()             # 获取选项列表
    if request.method == 'GET'and not asset:
        goods = Goods.objects.all()  # 查询情况下
        a = {}
        for i in goods:
            cc = 0
            stoc = StockControl.objects.filter(goods_id=i.id).values('number')
            for o in range(0, len(stoc)):
                c = stoc[o]['number']
                cc += c
            a[i.name] = cc
        return render(request, 'in/in.html', {'a': a, 'asset_name': asset_name})
    elif request.method == 'GET'and asset:
        goods = Goods.objects.get(pk=asset)
        a = {}
        stoc = StockControl.objects.filter(goods_id=asset).values('number')
        cc = 0
        for o in range(0, len(stoc)):
            c = stoc[o]['number']
            cc += c
        a[goods.name] = cc
        asset = int(asset)
        return render(request, 'in/in.html', {'a': a, 'asset_name': asset_name, 'asset': asset})
    else:
        return HttpResponse('加载超时')
